package com.bxshey.happyTown.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @作者：不想说话而已
 * @Date：2023/12/4 0:20
 */

@Service
@Transactional
@Slf4j
public class RedisUtils {

    //存储用户点赞信息的前缀
    public static final String SUPPORT = "support:";

    //存储用户登录验证码的前缀
    public static final String LOGIN_CAPTCHA = "captcha:";
    //存储用户注册验证码的前缀
    public static final String LOGIN_ENROLL = "enroll:";
    //新评论信息前缀
    public static final String COMMENTS = "comments:";

    @Autowired
    public StringRedisTemplate redisTemplate;


    //存入值
    public <T> void addValue(String prefix, T id, String value,Long time){
        String s = String.valueOf(id);
        redisTemplate.opsForValue().set(prefix.concat(s),value, time,TimeUnit.SECONDS);
    }

    //检查是否在该值
    public <T> Boolean checkValue(String prefix, T id, String value){
        String s = String.valueOf(id);
        return Objects.nonNull(redisTemplate.opsForValue().get(prefix.concat(s)));
    }


    /*
     *set类型  检查是否存在key
     * prefix: 前缀
     * id：唯一标识
     * value：值
     */
    public <T> Boolean checkSetValue(String prefix,T id,String value){
        String s = String.valueOf(id);
        return redisTemplate.opsForSet().isMember(prefix.concat(s), value);
    }

    //set类型 存入单个值
    public <T> void addSetValue(String prefix, T id, String value){
        String s = String.valueOf(id);
        redisTemplate.opsForSet().add(prefix.concat(s), value);
    }

    //set类型 删除单个值
    public <T> void delSetValue(String prefix, T id, String value){
        String s = String.valueOf(id);
        redisTemplate.opsForSet().remove(prefix.concat(s), value);
    }

    //set类型 存入多个值
    public <T> void addSetValue(String prefix, T id, String[] value){
        String s = String.valueOf(id);
        redisTemplate.opsForSet().add(prefix.concat(s), value);
    }


    //list类型，从左侧添加单个值
    public <T> void addListValue(String prefix, T id, String value){
        redisTemplate.opsForList().leftPush(prefix.concat(String.valueOf(id)),value);
    }

    //list类型，获取指定个数的值
    public <T> List<String> getListValue(String prefix, T id, Long start, Long end){
       return redisTemplate.opsForList().range(prefix.concat(String.valueOf(id)),start,end);
    }


    //list类型，获取长度
    public <T> Long getSize(String prefix, T id){
        return redisTemplate.opsForList().size(prefix.concat((String) id));
    }

    //list类型，删除全部
    public <T> void delListValue(String prefix, T id){
        String key = prefix.concat(String.valueOf(id));
        Long size = redisTemplate.opsForList().size(key);
        if(Objects.nonNull(size) && size>0){
            redisTemplate.opsForList().trim(key,size,size);
        }
    }

    //list类型，删除全部
    public <T> Long delListValue(String prefix, T id,String value){
        String key = prefix.concat(String.valueOf(id));
        return  redisTemplate.opsForList().remove(key,1,value);
    }


}
